package aex.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import aex.domain.ArticleCategory;
import aex.service.ArticleCategoryService;
import aex.util.CU;
import aex.util.PageBean;

import com.opensymphony.xwork2.ModelDriven;

@Controller("CategoryManagerAction")
@Scope("prototype")
public class CategoryManagerAction extends BaseActon implements ModelDriven<ArticleCategory> {

	private static final long serialVersionUID = 1L;


	@Resource(name = ArticleCategoryService.SERVICE_NAME)
	ArticleCategoryService acService;

	ArticleCategory category = new ArticleCategory();

	public CategoryManagerAction() {
		CU.deBugPrintln("CategoryManagerAction 被创建");
	}

	@Override
	public ArticleCategory getModel() {
		return category;
	}


	public String categoryList(){
		PageBean<ArticleCategory> pageBean = new PageBean<ArticleCategory>();
		PageBean.initPageBean(pageBean, request,"");
		pageBean = acService.findAll(pageBean);
		request.setAttribute("pageBean", pageBean);
		return "categoryList";
	}
	

	/**
	 * 删除单个栏目
	 * @return
	 */
	public String deleteCategory(){
		int id=Integer.parseInt(request.getParameter("id"));
		acService.deleteById(id);
		ajaxPrint("删除成功！");
		return null;
	}
	
	/**
	 * 删除多个栏目
	 * @return
	 */
	public String deleteCategorys(){
		String[] ids=request.getParameter("ids").split(",");
		for (int i = 0; i < ids.length; i++) {
				acService.deleteById(ids[i]);
		}
		ajaxPrint("成功删除"+ids.length+"条数据！");
		return null;
	}
	
	public String addCategory(){
		
		category.setAcOrderBy(acService.findCategoryCount() + 1);
		category.setAcCount(0);
		category.setAcUserNum(0);
		category.setAcPhoto(request.getParameter("imgPath"));
		acService.save(category);
		return CU.goMsgPage(request, "成功添加一个小组！", "ok", "manage/categoryManager_categoryList.do");
	}
	
	public String editorCategory(){
		request.setAttribute("category", acService.findByID(request.getParameter("id")));
		return "editorCategory";
	}
	
	
	public String updateCategory(){
		
		ArticleCategory oldCategory= acService.findByID(category.getAcId());
		category.setAcOrderBy(oldCategory.getAcOrderBy());
		category.setAcCount(oldCategory.getAcCount());
		category.setAcUserNum(oldCategory.getAcUserNum());
		category.setAcPhoto(request.getParameter("imgPath"));
		acService.update(category);
		return CU.goMsgPage(request, "小组信息已经更新！", "ok", "manage/categoryManager_categoryList.do");
	}
	
	
	public String updateCategoryOrderBy(){
		
		try{
			String[] seq=request.getParameter("cateSeq").split(",");
			//重新排序
			List<ArticleCategory> seamCateList=acService.findAllNoPage();
			for (int i = 0; i < seq.length; i++) {
				
				int newSeq=Integer.parseInt(seq[i]);
				
				if(seamCateList.get(i).getAcOrderBy()!=newSeq){
					
					CU.deBugPrintln("更新"+seamCateList.get(i).getAcName()+" "+seamCateList.get(i).getAcOrderBy()+"---->"+newSeq);
					
					seamCateList.get(i).setAcOrderBy(newSeq);
					
					acService.update(seamCateList.get(i));
				}
			}
			response.getWriter().write("{\"info\":\"修改成功！\",\"status\":\"y\"}");

		} catch (IOException e) {
			try {
				response.getWriter().write(
						"{\"info\":\"修改失败\",\"status\":\"y\"}");
			} catch (IOException e1) {
			}
		}
		return null;
	}
	
	public String orderByCategory(){
		request.setAttribute("seamCates", acService.findAllNoPage());
		return "orderByCategory";
	}
	
	

}
